<?php 
include_once "function_xml_basic.php";

function GetXmlVlanList()
{
	$query_string = "/MINI/NETWORK/VLANS/VLAN";

	$ret_array = array();
	$results = GetAttribute($query_string);
	$i = 0;
	foreach ($results as $result)
	{
		$ret_array[$i]['VlanId'] = $result->getAttribute('VlanId');
                $ret_array[$i]['Desc'] = $result->getAttribute('Desc');
                $ret_array[$i]['Ip'] = $result->getAttribute('Ip');
                $ret_array[$i]['Mask'] = $result->getAttribute('Mask');
                $ret_array[$i]['WorkModel'] = $result->getAttribute('WorkModel');
                $ret_array[$i]['Dev'] = $result->getAttribute('Dev');
                $ret_array[$i]['Protocol'] = $result->getAttribute('Protocol');
                $ret_array[$i]['Enabled'] = $result->getAttribute('Enabled');
		
                $i ++;
	}
	return $ret_array;
}

/*$ret_array = GetXmlVlanList();
foreach ($ret_array as $ret)
{
	print $ret['Name'];
	print $ret['VlanId'];
}*/

function GetXmlVlanNameByInfo()
{
	$ret_name_array = array ();
	$ret_array = GetXmlVlanList();
	$i = 0;
	foreach ($ret_array as $ret)
	{
		$ret_name_array[$i ++]  =  $ret['Dev']. '.'. $ret['VlanId'];
	}
	return $ret_name_array;
}
#$ret_list = GetXmlVlanNameByInfo();
#foreach ($ret_list as $ret)
#{
#	print $ret;
#}

function GetXmlSpecialVlanInfo($dev, $vlanid)
{
	$query_string = "/MINI/NETWORK/VLANS/VLAN[@Dev='". $dev. "' and @VlanId='". $vlanid. "']";
	$ret_array = array();
	$results = GetAttribute($query_string);
	foreach ($results as $ret)
	{
		$ret_array['VlanId'] = $ret->getAttribute('VlanId'); 
		$ret_array['Desc'] = $ret->getAttribute('Desc'); 
		$ret_array['Dev'] = $ret->getAttribute('Dev'); 
		$ret_array['WorkModel'] = $ret->getAttribute('WorkModel'); 
		$ret_array['Ip'] = $ret->getAttribute('Ip'); 
		$ret_array['Mask'] = $ret->getAttribute('Mask'); 
		$ret_array['Protocol'] = $ret->getAttribute('Protocol'); 
		$ret_array['Enabled'] = $ret->getAttribute('Enabled'); 
	}
	return $ret_array;
}
#$ret = GetXmlSpecialVlanInfo('eth3', '4');
#print $ret['VlanId'];


function DelXmlNetDevOneVlan($dev, $vlanid)
{
	$query_string = "/MINI/NETWORK/VLANS/VLAN[@Dev='". $dev. "' and @VlanId='". $vlanid. "']";
	DelSpecialNode($query_string);
	ApplySysAllVlanDevInfo();
}

function DelXmlNetDevAllVlan()
{
	$query_string = "/MINI/NETWORK/VLANS/VLAN";
	DelSpecialNode($query_string);
	FlushSysAllVlanConfig();
}

function AppendXmlNetDevOneVlan($Valu_array)
{
	$node_name = "VLAN";
	$query_string = "/MINI/NETWORK/VLANS";
	
	AppendAllAttrOfNode($query_string, $node_name, $Valu_array);
	ApplySysAllVlanDevInfo();
}

function EditXmlNetDevVlan($dev, $vlanid, $in_array)
{
	$query_string = "/MINI/NETWORK/VLANS/VLAN[@Dev='". $dev. "' and @VlanId='". $vlanid. "']";
	EditAllAttrOfNode($query_string, $in_array);

	ApplySysAllVlanDevInfo();
}

include_once "function_misc.php";

function GetVlanDevListFromSys()
{
	$command_ifconfig = "/sbin/ifconfig ";

	$command = $command_ifconfig. " -a | grep eth[0-9] | awk '{print $1}' | grep \"\.\" ";
	$ret_string = shell_exec($command);
	
	$ret_array = explode("\n", $ret_string);

        #foreach ($ret_array as $ret)
        #{
        #       print "[". $ret. "]\n";
        #}
        return $ret_array;
}
function ApplySysAllVlanDevInfo()
{
	$command = "";
	$command_vconfig= "/usr/local/sbin/vconfig ";
	$command_ifconfig = "/sbin/ifconfig ";
	
	FlushSysAllVlanConfig();
	
	$ret_v_list = GetXmlVlanList();	
	
	$command = $command_vconfig. " set_name_type DEV_PLUS_VID_NO_PAD; ";

	foreach($ret_v_list as $v)
	{

		$command .= $command_vconfig. " add ". $v['Dev']. " ". $v['VlanId']. " ;";
		$command .= $command_ifconfig. $v['Dev']. ".". $v['VlanId']. " ". $v['Ip']. " netmask ". $v['Mask']. " ; ";
		$command .= $command_ifconfig. $v['Dev']. ".".$v['VlanId']. " up ; ";
	}
	//print $command."\n\n";
	$ret_string = shell_exec($command);
	return $ret_string;
}

function FlushSysAllVlanConfig()
{
	$ret_array_vlan_list = GetVlanDevListFromSys();

	$command = "";
	$command_vconfig = "/usr/local/sbin/vconfig ";
	$command_ifconfig = "/sbin/ifconfig ";

	foreach ($ret_array_vlan_list as $v)
	{
		if ($v == "")
			continue;
		
		$command .= $command_ifconfig. " ". $v. " 0 ;";
		$command .= $command_vconfig. " rem ". $v. " ; ";
	}
	//print $command. "\n\n";
	$ret_string = shell_exec($command);
	return $ret_string;
}

?>
